package pt.elvis.revanweb.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Image;

/**
 *@author	<b>Elvis Pestana</b>
 *@author	It is an anchor that surrounds an image.
 *@param	imageURL An URL giving the base location of the image
 *  
 *                        
 *@param	caption The description of the image that appears on mouse hover.
 *
 *@return	Returns an Image object that is wrapped with an HTML anchor element
 *
 *@see		Image
 *@see		Anchor
 *@see		<a href="http://peterlairdtech.blogspot.com/2010/04/imageanchor-sample-code-gwt-anchor.html">Found here - External</a>
 **/

public class ImageAnchor extends Anchor {	
	HTMLPanel html;
	String caption;
	Image img;
	final int idBookmark;
	final String spanTag = "<span class=\"desc\"";
	
	public ImageAnchor(String imageURL, String caption, int idBookmark, String title) {
		 this.img = new Image(imageURL);
		 this.img.setTitle(caption);
		 this.caption = title;
		 this.idBookmark = idBookmark;
		 setTarget("_blank");
		 buildImage();
    }
    
	public void resetImage(String imageURL) {
		 this.img.setUrl(imageURL);
		 this.img.setTitle(caption);
		 DOM.insertBefore(getElement(), html.getElement(), DOM.getFirstChild(getElement()));
	     DOM.insertChild(getElement(), img.getElement(), 5);
	}
	
    private void buildImage() {
    	html = new HTMLPanel(spanTag, caption);
        DOM.insertBefore(getElement(), html.getElement(), DOM.getFirstChild(getElement()));
        DOM.insertChild(getElement(), img.getElement(), 5);
    }
    
    public void setImageCaption(String caption) {
    	this.caption = caption;
    	html.getElement().setInnerText(this.caption);
    }
    
    public void refreshImage(String url) {
    	img.setUrl(url + "?r=" + Random.nextInt());
    }

	public int getIdBookmark() {
		return idBookmark;
	}
}
